package com.ftg.learn.lesson4;

import java.util.Arrays;

/**
 * 学习一下数组帮助类
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/6/19 下午3:51
 */
public class LearnArray02WithArrays {


    /**
     * 数组是无序
     * 给定一个数组 arrs问这里面有有没有index这个内容
     * @param arrs  数组
     * @param index 下标
     */
    public static boolean findEle(int[] arrs,int index){
        for (int i = 0; i < arrs.length; i++) {
            if(arrs[i] == index){
                return true;
            }
        }
        return false;
    }

    /**
     * 二分查找法
     */
    public static boolean binarySearch(int[] arrs,int index){
        int start = 0;
        int end = arrs.length;
        while(start < end){
            int pivot = (end + start) >> 1;
            if(arrs[pivot] == index){
                return true;
            }else if(arrs[pivot] > index){
                end = pivot - 1;
            }else{
                start = pivot + 1;
            }
        }
        return false;
    }


    public static void main(String[] args) {
        int[] arrs = new int[]{1,2,3,4,5};

        int[] arrs3 = new int[6];
        arrs3[0] =99;
        System.arraycopy(arrs,0,arrs3,3,3);

        System.out.println(Arrays.toString(arrs3));



        int[] arrs1 = Arrays.copyOf(arrs,15);

        int[] arrs2 = Arrays.copyOfRange(arrs,1,4);
        System.out.println(Arrays.toString(arrs2));

        System.out.println(Arrays.toString(arrs1));

        Arrays.sort(arrs);
        //引用类型
//        Arrays.deepEquals()
        System.out.println(Arrays.binarySearch(arrs, 7));
        System.out.println(binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 91));
        System.out.println(findEle(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 9));
    }

}
